<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-26
 * Time: 18:06
 */

namespace app\admin\controller;


use app\base\BaseController;
use app\base\model\Classify as fenlei;
use app\base\validate\PubValidate;
use think\Request;

class Classify extends BaseController
{
    /***
     * 输出用户管理页面视图
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }
    /***
     * 输出用户管理页面数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ajax()
    {

        $request = Request::instance();
        if (!$request->isAjax()) {
            return ['code'=>-1,'msg'=>'请求方式错误'];
        }
        $classify = new fenlei();
        $limit = $request->param('limit');
        $offset = $limit * $request->param('page')-$limit;

        $total = $classify->count('id');
        $data = $classify->limit($offset,$limit)->select();
        return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $data];
    }
    public function dell()
    {
        $request = Request::instance();
        $id = $request->has('id')?$request->param('id'):false;
        if (!$id) {
            return ['code' => -1, 'msg' => '参数错误'];
        }
        fenlei::destroy($id);
        return ['code' => 1, 'msg' => '删除成功'];


    }



    public function add()
    {
        $request = Request::instance();
        $name = $request->has('name')?$request->param('name'):false;
        if (!$name) {
            return ['code' => -1, 'msg' => '参数错误'];
        }
        fenlei::create(['name' => $name]);
        return ['code' => 1, 'msg' => '新增成功'];


    }


}